# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: yuxuan

from absl import (flags, app)

flags.DEFINE_string('pnl_file', '', '')

FLAGS = flags.FLAGS


def main(args):
  with open(FLAGS.pnl_file, 'r') as fi:
    lines = [line.strip() for line in fi]
  total_pnl = 0.0
  volumes = dict()
  content = []
  drop = False
  for line_no, line in enumerate(lines):
    if line.lower().startswith('pnl'):
      total_pnl += float(lines[line_no + 1])
    elif line.lower().startswith('volume') and 'breakdown' not in line.lower():
      volume_key = line.split(' ')[1].upper()
      a_volume = float(lines[line_no + 1])
      volumes[volume_key] = volumes.get(volume_key, 0.0) + a_volume
    elif line.lower().startswith('volume breakdown'):
      drop = True
    if drop:
      if not line:
        drop = False
    else:
      content.append(line)
  print('\n'.join(content))
  print('\nTotal Pnl: %f' % total_pnl)
  for volume_key, volume in volumes.items():
    print('Total Volume %s\n%f' % (volume_key, volume))


if __name__ == '__main__':
  app.run(main)
